
var common_option  = null; 

function randerModal(title,id){
        $("#table_detail .modal-title").text(title);
        var pk_id =id ; //  ; 
        var data_item = null ; 
        if(pk_id==-1){
          data_item={};
        }else{
          for(var i=0; i<data_list.length; i++ ){
            if(data_list[i][pk_key]==pk_id){
              data_item = data_list[i] ; 
              break ; 
            }
          }
        var html = '';
        html += '<div class="form-group">';
        html += '<label class="col-sm-2 control-label">id:</label>';
        html += '<div class="col-sm-6">';
        html += '<p class="form-control-static">'+pk_id+'</p>';
        html += '</div>';
        html += '</div>';
      $("#table_detail .modal-body #list_form_in").append(html);
        }
        if(data_item!=null){ //这就是 要操作的数据
          cur_data = data_item ; 
          for(var c = 0 ; c < column_title.length ; c ++){
              var col = column_title[c] ; 
              var name = data_item[col['key']] ; 
              if(col.editable == "1"){
                if(col.type == "number"){
                  editable_number(col,name);
                }else if(col.type == "text"){
                  editable_text(col,name);
                }else if(col.type == "time"){
                  editable_time(col,name);
                }else if(col.type == "enum"){
                  // var name = data_item[col['show_key']] ; 
                  // editable_enum(col,name);
                }else if(col.type == "custom"){
                  editable_custom(col,name);
                }else if(col.type == "array"){
                  editable_array(col,name);
                }else if(col.type == "img"){
                  editable_img(col,data_item[col['show_key']]);
                }else if(col.type == "richtext"){
                  editable_richtext(col,name);
                }else if(col.type == "location"){
                  editable_location(col,name,data_item);
                }
             }
          }
          initEnumCol(data_item,column_title);

          
        } 
    }
  var is_readonly = false; 
  var is_rander_select = false;
  var is_modal_show = false;
  var is_ajax_over = true ;  
  var cur_data = null ; 
  var id_name = pk_key;
  var submit_table_url = null;

//----------------------------------------------------------------------
$(function(){
  $(".new_file_coupon").click(function(){ //新建
    is_readonly = false ; 
    randerModal('新建',-1);
    // $(".submit_btn").attr("data-type","create");
    if($("#pageTitle").hasClass("nourl")){
      submit_table_url = "/"+data_name+"/create";
    }else{
      submit_table_url = "/ajax/call/"+data_name+"/create";
    }
    
  })
  $('.edit_btn').click(function(){  //编辑按钮
      var id = $(this).attr("data-pk");
      is_readonly = false ; 
      is_rander_select= false ; 
      randerModal('修改',$(this).attr("data-pk"));
      // $(".submit_btn").attr("data-type","update");
      if($("#pageTitle").hasClass("nourl")){
        submit_table_url = "/"+data_name+"/update?"+id_name+"="+id;
      }else{
        submit_table_url = "/ajax/call/"+data_name+"/update?"+id_name+"="+id;
      }
    });
    $('.detail_btn').click(function(){  //详情按钮
      is_readonly = true ; 
      is_rander_select= true ; 
      randerModal('查看',$(this).attr("data-pk"));
      $(".submit_btn").hide();
    });
    $(".del_btn").click(function(){ //删除
      var id = $(this).attr("data-pk");
      swal({
          title: "确定要删除这条信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "删除",
          cancelButtonText: "取消",
          closeOnConfirm: false
      }, function () {
          tabDel(id);
      });
    });
$('#table_detail').on('shown.bs.modal', function () { //表单数据加载完
  is_modal_show = true ; 
  doRanderSelect();
  if(common_option!=null){
    setTimeout(function(){
      if(common_option.complete!=undefined && common_option.complete!=null && typeof common_option.complete =='function'){
        try{
          common_option.complete();
        }catch(e){

        }
      }
    },3000)
    
  }

  
});
$('#table_detail').on('show.bs.modal', function () {
  $(".rander_model_input").attr('readOnly',is_readonly);
  $(".rander_model_input").attr('disabled',is_readonly);

  
  is_modal_show = false ;
  is_ajax_over = true ; 
});

  $(".close").click(function(){ //表格上面关闭按钮
    $(".submit_btn").show();
    $("#table_detail .modal-body #list_form_in").html("");
  });
  $(".table_close").click(function(){//表格下面关闭按钮
    $(this).attr("aria-hidden","true").attr("data-dismiss","modal")
    $("#table_detail .modal-body #list_form_in").html("");
    $(".submit_btn").show();
  });
  $(".submit_btn").click(function(){  //提交按钮
    $("#signupForm").validate({
        submitHandler:function(form){
           doSubmit(submit_table_url);
        }    
    });
  })
  $(".excel_submit_btn").click(function(){
    var loc_search = location.search;
    var url = null;
    var excel_name = $("#excel_name").val();
    if(loc_search==null || loc_search==""){
      url = "/excel/call"+location.pathname+"?excel_name="+excel_name;
    }else{
      url = "/excel/call"+location.pathname+loc_search+"&excel_name="+excel_name;
    }
    if(excel_name){
      location.href = url;
      setTimeout(function(){
        location.reload();
      },1000)
    }else{
     $("#excel_name").focus();
    }
    
  })
});    
//---------------------------

    function tabDel(id){  //删除
      var url = null;
      if($("#pageTitle").hasClass("nourl")){
        url = "/"+data_name+"/delete?"+id_name+"="+id;
      }else{
        url = "/ajax/call/"+data_name+"/delete?"+id_name+"="+id;
      }
    $.ajax({
      url:url,
      success:function(data){
        if(data.errno!=undefined && data.errno != 0 && typeof data != "string"){
            swal("很遗憾", data.errmsg+",请稍后重试");
          }else{
            if(typeof data == "string" ){
                if(data.indexOf("nopermission_yes") >= 0){
                  swal("系统提示", "您没有权限操作！", "error");
                  return;
                }
            }
            swal("删除成功！", "您已经删除了这条信息。", "success");
            location.reload();
          }
      }
    })
    
  }
  function doSubmit(url){  //提交
    $('#signupForm').ajaxSubmit({  
        type:"post",  //提交方式  
        url:url, //请求url  
        success:function(data){ //提交成功的回调函数  
          if(data.errno!=undefined && data.errno != 0 && typeof data != "string"){
            swal("很遗憾", data.errmsg+",请稍后重试");
          }else{
            if(typeof data == "string" ){
                if(data.indexOf("nopermission_yes") >= 0){
                  swal("系统提示", "您没有权限操作！", "error");
                  return;
                }
            }
            swal("恭喜您！", "提交成功啦。");
            location.reload();
          }
          
        }
    });
  }
//------------------------------------------渲染html元素---------------------------------------
function editable_richtext(col,val){  //渲染富文本类型的
  val = val==undefined?'':val; 
  var is_must = null;
  var required = null;
  if(col.is_must && col.is_must == 1){
    is_must = '<span class="is_must">*</span>';
    required = 'required="true"';
  }else{
    is_must = '<span></span>';
    required = '';
  }

  var html = '';
      html += '<div class="form-group">';
      html += '<label class="col-sm-2 control-label"">'+col.name+':</label>';
      html += '<div class="col-sm-10 ">';
      html += '<textarea name="'+col.key+'" class="rander_model_input" rows="20" id="'+col.key+'" '+required+' runat="server" readonly="readonly" >'+val+'</textarea>';
      html += '</div>';
      html += '</div>';

  $("#table_detail .modal-body #list_form_in").append(html);
}
function editable_location(col,val,data_item){ //渲染地图类型的
  val = val==undefined?'':val; 
  var is_must = null;
  var required = null;
  if(col.is_must && col.is_must == 1){
    is_must = '<span class="is_must">*</span>';
    required = 'required="true"';
  }else{
    is_must = '<span></span>';
    required = '';
  }

  var html = '';
      html += '<div class="form-group">';
      html += '<label class="col-sm-2 control-label">'+col.name+':</label>';
      html += '<div class="col-sm-10 ">';


      html += '<div class="row" style="margin-bottom: 10px;">';
      html += '<div class="col-sm-4">';
      html += '<div class="input-group">';
      html += '<span id="id_mark_span" class="input-group-addon input-sm">轻点地图标记商家位置</span>';
      html += '<button class="btn form-control rander_model_input" type="button" onclick="switchMarkSite()" id="id_btn_vender_site">开启标记位置</button>';
      html += '</div>';
      html += '</div>';
      html += '<div id="r-result" class="col-sm-4">';
      html += '<div class="input-group">';
      html += '<span class="input-group-addon input-sm">位置搜索</span>';
      html += '<input type="text" id="suggestId" class="form-control rander_model_input" size="20" />';
      html += '</div>';
      html += '</div>';
      html += '</div>';
      html += '<div id="searchResultPanel" style="border: 1px solid #C0C0C0; width: 150px; height: auto; display: none;"></div>';
      html += '<div id="allmap" style="width: 100%; height:500px; margin-top: 20px;"></div>';
      html += '<input type="hidden" name="'+col.customMap.lng+'" id="'+col.customMap.lng+'" value="'+data_item[col.customMap.lng]+'" class="form-control" size="20" />';
      html += '<input type="hidden" name="'+col.customMap.lat+'" id="'+col.customMap.lat+'" value="'+data_item[col.customMap.lat]+'" class="form-control" size="20" />';


      html += '</div>';
      html += '</div>';

  $("#table_detail .modal-body #list_form_in").append(html);

    



}
var isMark = false; 
      function switchMarkSite(){
        isMark=!isMark;
        if(isMark){
          $("#id_btn_vender_site").html("开启标记位置");
          $("#id_btn_vender_site").removeClass("btn-danger");
          $("#id_btn_vender_site").addClass("btn-success");
        }else{
          $("#id_btn_vender_site").html("关闭标记位置");
          $("#id_btn_vender_site").addClass("btn-danger");
          $("#id_btn_vender_site").removeClass("btn-success");
        }
      }
function editable_img(col,val){  //渲染图片类型的
  val = val==undefined?'':val; 
  var is_must = null;
  var required = null;
  if(col.is_must && col.is_must == 1){
    is_must = '<span class="is_must">*</span>';
    required = 'required="true"';
  }else{
    is_must = '<span></span>';
    required = '';
  }

  var html = '';
      html += '<div class="form-group">';
      html += '<label class="col-sm-2 control-label"">'+col.name+':</label>';
      html += '<div class="col-sm-6">';
      html += '<input id="'+col.key+'" class="rander_model_input" data-url="'+val+'" name="image_file" value="'+val+'" type="file" accept="image/*">';   
      html += '</div>';      
      html += '<input id="'+col.key+'_ed" '+required+' type="hidden" name="'+col.key+'" value="'+val+'" accept="image/*">';   
      html += '</div>';



$("#table_detail .modal-body #list_form_in").append(html);


}
function editable_array(col,val){
    val = val==undefined?'':val; 
    var ajaxUrl = col.customMap.url ; 
    var fdStart = ajaxUrl.indexOf("/");

    var url = null;
        if(fdStart == 0){
          url = ajaxUrl;
        }else{
          url = "/ajax/call/"+ajaxUrl;
        }
        $.ajax({
          url:url,
          type:'get',
          success:function(res){
              if(res.data_list!=undefined){
                  editable_array2(col,val,res.data_list);
              }
          }    

        });
}
function editable_array2(col,val,dlist){
    val = val==undefined?'':val; 
    var is_must = null;
    var required = null;

      if(col.is_must && col.is_must == 1){
        is_must = '<span class="is_must">*</span>';
        required = 'required="true"';
      }else{
        is_must = '<span></span>';
        required = '';
      }
      var disStr = is_readonly?'disabled=""':'';
      var html = '';
          html += '<div class="form-group">';
          html += '<label class="col-sm-2 control-label"">'+col.name+':</label>';
          html += '<div class="col-sm-6">';
          html += '<select id="'+col.key+'" name="'+col.key+'[]" '+disStr+' data-placeholder="请选择'+col.name+'" class="chosen-select col-sm-12" multiple tabindex="4">';
          // html += '<option value="-1">全部</option>';
          
          for(var i =0 ; i < dlist.length ; i ++){
              var dd = dlist[i] ; 
              var sed = false ; 
              if(val!=undefined&&val!=null&&val.length>0){
                for(var z = 0 ; z < val.length ; z++){
                    var v = val[z] ;
                    if(v==dd[col.customMap.option_value]){
                      sed = true ; 
                      break ; 
                    }
                }
              }
              
              //val == dd[col.customMap.option_value]
              if(sed){
                html += '<option value="'+dd[col.customMap.option_value]+'" selected hassubinfo="true">'+dd[col.customMap.option_text]+'</option>';
              }else{
                html += '<option value="'+dd[col.customMap.option_value]+'" hassubinfo="true">'+dd[col.customMap.option_text]+'</option>';
              }
          }
          html += '</select>';
          html += '</div>';
          html += '</div>';

$("#table_detail .modal-body #list_form_in").append(html);
}
function editable_number(col,val){     //渲染浮层number类型的函数
      val = val==undefined?'':val; 
      var is_must = null;
      var required = null;
        if(col.is_must && col.is_must == 1){
          is_must = '<span class="is_must">*</span>';
          required = 'required="true"';
        }else{
          is_must = '<span></span>';
          required = '';
        }

      var html = '';
          html += '<div class="form-group">';
          html += '<label class="col-sm-2 control-label">'+col.name+':</label>';
          html += '<div class="col-sm-6">';
          html += '<input id="'+col.key+'" name="'+col.key+'" type="number" class="form-control form-control rander_model_input" value="'+val+'" placeholder="请输入'+col.name+'" '+required+'>';
          html += '</div>'+is_must+'';
          html += '</div>';
      $("#table_detail .modal-body #list_form_in").append(html);
  }
  function editable_text(col,val){     //渲染浮层text类型的函数
      val = val==undefined?'':val; 
      var is_must = null;
      var required = null;
        if(col.is_must && col.is_must == 1){
          is_must = '<span class="is_must">*</span>';
          required = 'required="true"';
        }else{
          is_must = '<span></span>';
          required = '';
        }
      var html = '';
          html += '<div class="form-group">';
          html += '<label class="col-sm-2 control-label">'+col.name+':</label>';
          html += '<div class="col-sm-6">';
          html += '<input id="'+col.key+'" name="'+col.key+'" type="text" class="form-control form-control rander_model_input" value="'+val+'" placeholder="请输入'+col.name+'" '+required+' >';
          html += '</div>'+is_must+'';
          html += '</div>';
      $("#table_detail .modal-body #list_form_in").append(html);
  }
  function editable_time(col,val){       //渲染浮层time类型的函数
     val = val==undefined?'':val; 


      var html = '';

        html += '<div class="form-group">';
        html += '<label class="col-sm-2 control-label">'+col.name+':</label>';
        html += '<div class="col-sm-6">';
        html += '<input id="'+col.key+'" class="form-control layer-date rander_model_input time_widget_laydate" data-hid-key="hid_'+col.key+'" placeholder="请选择日期" value="'+val+'"  onclick="laydate({istime: true, format:\'YYYY-MM-DD hh:mm:ss\',choose: function(dates){date_widget_choose(dates,\'hid_'+col.key+'\');}})">';
        html += '<input name="'+col.key+'" id="hid_'+col.key+'" class="data_hidden" type="hidden">';
        html += '</div>';
        html += '</div>';


      $("#table_detail .modal-body #list_form_in").append(html);
      date_widget_choose($("#"+col.key).val(),'hid_'+col.key);
  }

  function editable_custom(col,val){       //渲染浮层custom类型的函数
      // var html = '<div>'+'---custom---'+getCustomVal(col,name)+'</div>';
      val = val==undefined?'':val; 
      var is_must = null;
      var required = null;
        if(col.is_must && col.is_must == 1){
          is_must = '<span class="is_must">*</span>';
          required = 'required="true"';
        }else{
          is_must = '<span></span>';
          required = '';
        }
      var html = '';
          html+= '<div class="form-group">';
          html+= '<label class="col-sm-2 control-label">'+col.name+':</label>  ';
          html+= '<div class="col-sm-6">';
          html+= '<select id="'+col.key+'" name="'+col.key+'" class="col-sm-12 form-control form-control m-b rander_model_input" width="" tabindex="" '+required+'>';
          html+= getCustomOption(col,val);
          html+= '</select>';
          html+= '</div>'+is_must+'';
          html+= '</div>';
      $("#table_detail .modal-body #list_form_in").append(html);
  }

//---------------------------------------------------------------------------------

function getCustomVal(col,val){  //获取当前custom类型选择的值
    if(col!=null&&col.customMap!=undefined &&col.customMap!=null){
        return col.customMap[val] ; 
    }else{
      return "n/a";
    }
  }
  function getCustomOption(col,val){ //获取custom类型所有的值
    var htmStr = "" ; 
    if(col!=null&&col.customMap!=undefined &&col.customMap!=null){
        for(var k in col.customMap){
          if(k==val){
              htmStr+="<option selected='true' value='"+k+"'>"+col.customMap[k]+"</option>"
          }else{
             htmStr+="<option value='"+k+"'>"+col.customMap[k]+"</option>"
          }
         }
      return htmStr;    
    }else{
      return null;
    }
  }

//-------------------------------------渲染enum类型的二级联动的系列操作---------

function doRanderSelect(){
  if(is_modal_show&&is_ajax_over){
     $('.chosen-select').chosen({});
     is_modal_show = false ;
     is_ajax_over = false; 
     $(".dataTables-example").dataTable();
     $(".chosen-select").change(function(){
        

        var my_key = $(this).attr("data-key") ; 

        // var val =  $(this).val();
        // var url = $(".parent_"+my_key).attr('data-url');
        // var params_key = $(".parent_"+my_key).attr('data-params') ; 
        // $(".parent_"+my_key).hide();
        // reRanderSelect(url,params_key,val,my_key);

        if($('.parent_'+my_key).length>0){
          var col_ary = [] ; 
           for(var i = 0  ; i < column_title.length ; i ++ ){  //有 parent 的 
              var col = column_title[i] ; 
              if(col.type=='enum'){
                if(col.customMap.enum_parent!=undefined&&col.customMap.enum_parent!=null){
                  col_ary.push(col);
                }
              }
            }
            $('.parent_'+my_key).remove();
            is_modal_show = true ; 
            reanderEnumCol(cur_data,col_ary);
        }
     });

  }
  

}

function initEnumCol(dt,ct){
  var col_ary = [] ; 

  for(var i = 0  ; i < ct.length ; i ++ ){  //有 parent 的 
    var col = ct[i] ; 
    if(col.type=='enum'){
      if(col.customMap.enum_parent!=undefined&&col.customMap.enum_parent!=null){
        col_ary.push(col);
      }
    }
  }
  for(var i = 0  ; i < ct.length ; i ++ ){  //没有parent 的 
    var col = ct[i] ; 
    if(col.type=='enum'){
      if(col.customMap.enum_parent==undefined||col.customMap.enum_parent==null){
        col_ary.push(col);
      }
    }
  }
  reanderEnumCol(dt,col_ary);
}




function reanderEnumCol(data,col_ary){
  if(col_ary.length>0){
    var cl = col_ary.pop();
    if(cl.enum_parent==undefined||cl.enum_parent==null){
      randenBaseEnumSelect(cl,data[cl.key],function(){
        reanderEnumCol(data,col_ary);
      });
    }else{
      randenBaseEnumSelect(cl,data[cl.key],function(){
        reanderEnumCol(data,col_ary);
      });
    }
  }else{
    is_ajax_over = true ; 
    doRanderSelect();

  }
}


function editable_enum(col,val,dlist){

    var my_key = col.key ; 
    var par_key = (col.customMap.enum_parent!=undefined&&col.customMap.enum_parent!=null)?('parent_'+col.customMap.enum_parent):'';
    var params_key = (col.customMap.params!=undefined&&col.customMap.params!=null&&col.customMap.params.length>0)?col.customMap.params[0]:'';
    var is_must = null;
    var required = null;
        if(col.is_must && col.is_must == 1){
          is_must = '<span class="is_must">*</span>';
          required = 'required="true"';
        }else{
          is_must = '<span></span>';
          required = '';
        }
      var html = '';
      var disStr = is_readonly?'disabled=""':'';
          html += '<div class="form-group '+par_key+' ">';
          html += '<label class="col-sm-2 control-label">'+col.name+':</label>';
          html += '<div class="col-sm-6">';
          html += '<select id="'+col.key+'" class="chosen-select rander_model_input col-sm-12 '+my_key+'  "  '+disStr+' width="" data-key="'+my_key+'" data-params="'+params_key+'" data-parent="'+par_key+'" name="'+col.key+'" data-url="'+col.customMap.url+'" tabindex="2" '+required+'>';
          if(col.customMap.default_value != undefined){
            html += '<option value="'+col.customMap.default_value+'" >'+col.customMap.default_name+'</option>';
          }
          for(var i =0 ; i < dlist.length ; i ++){
              var dd = dlist[i] ; 
              if(val == dd[col.customMap.option_value]){
                html += '<option value="'+dd[col.customMap.option_value]+'" selected hassubinfo="true">'+dd[col.customMap.option_text]+'</option>';
              }else{
                html += '<option value="'+dd[col.customMap.option_value]+'" hassubinfo="true">'+dd[col.customMap.option_text]+'</option>';
              }
          }
          html += '</select>';
          html += '</div>'+is_must+'';
          html += '</div>';

      $("#table_detail .modal-body #list_form_in").append(html);
      
}

function randenBaseEnumSelect(col,val,callback){
    if(col!=null&&col.customMap!=undefined && col.customMap!=null){
      var par = {} ; 
      if(col.customMap.enum_parent!=undefined&&col.customMap.enum_parent!=null){
          if(col.customMap.params!=undefined&&col.customMap.params.length>0){
            par[col.customMap.params[0]] = $('.'+col.customMap.enum_parent).val() ; 
          }
      }
      is_ajax_over = false ; 
      var ajaxUrl = col.customMap.url ;

      var fdStart = ajaxUrl.indexOf("/");
      var url = null;
        if(fdStart == 0){
          url = ajaxUrl;
        }else{
          url = "/ajax/call/"+ajaxUrl;
        }
        $.ajax({
          url:url,
          type:'get',
          data:par,
          success:function(res){
              if(res.data_list!=undefined){
                  editable_enum(col,val,res.data_list);
              }
              if(callback!=null&&typeof callback == 'function'){
                callback();
              }
          }    

        });
      
  }


}

function date_widget_choose(dates,hid){
  dates = dates.replace(/[^0-9]/ig, "");
  $('#'+hid).val(dates); 
}






